<%@page import="java.text.DecimalFormat"%>
<%@page import="com.winexpress.product.Wine"%>
<%@page import="com.winexpress.product.Comment"%>
<%@page import="java.util.List"%>
<%@page import="com.winexpress.util.SessionAttributes"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>

<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>wineXpress online shop</title>
    </head>
    <body>
        <t:customerNavigation/>
        <div class="container">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">Shopping Cart</h1>
                    <ol class="breadcrumb">
                        <li><a href="/WineXpressClient/index.jsp">Home</a></li>
                        <li><class="active">Manage Shopping Cart</li>
                    </ol>
                </div>
            </div>

            <div style="width:100%'">        
                <form id="search" action="/WineXpressClient/searchManagement/shop" method="post">
                    <div style="float: left; width: 45%;">
                        <select class="form-control" name="seach">
                            <option value="searchByWineName">Wine name</option>
                            <option value="searcyByCategory">Category</option>
                            <option value="searchByCountry">Country</option>
                            <option value="searchByRegion">Region</option>
                            <option value="searchByVarietal">Varietal</option>
                            <option value="searchByVintage">Vintage</option>
                        </select>
                    </div>

                    <div style="float: left; width: 45%; padding-left: 10px; padding-right: 10px;">
                        <input type="text" class="form-control" name="key" placeholder="Enter keywords">
                    </div>
                    <button type="submit" class="btn btn-default" style="width: 10%;">Search</button>
                </form>
            </div>

            <% if (request.getAttribute("wineList") != null) {%>
            <table class="table table-striped table-bordered" style="text-align: center;">
                <tr>
                    <th></th>
                    <th style="text-align: center;">Name</th>
                    <th style="text-align: center;">Price</th>
                    <th style="text-align: center;">Qty</th>
                    <th style="text-align: center;">Line Summary</th>
                    <th></th>
                </tr>
                <% List<Wine> wineList = (List<Wine>) request.getAttribute("wineList"); %>
                <% List<Integer> quantity = (List<Integer>) request.getAttribute("quantity"); %>
                <% double sum = 0; %>
                <% for (int i = 0; i < wineList.size(); i++) {%>
                <% Wine wine = wineList.get(i);
                    String wineQuality = quantity.get(i).toString();
                    double payment = quantity.get(i) * wine.getPrice() * (100 - wine.getDiscount()) / 100;
                    boolean stock = wine.getStock() > 0;
                    if (!stock) {
                        wineQuality = "Sorry, it is out of stock";
                        payment = 0.00;
                    }%>
                <tr>
                    <td style="width: 15%; height: 100px;"><img class="img-responsive" style="margin-left: auto; margin-right: auto; vertical-align: middle; height: 100%;" src="<%out.print(wine.getImageUrl());%>" /></td>
                    <td style="text-align: center; vertical-align: middle;"><%out.print(wine.getName());%></td>

                    <% if (wine.getDiscount() != 0) {%>
                    <td style="text-align: center; vertical-align: middle;"><del>S$<%out.print(wine.getPrice());%></del>
                        <br/><b style="color: red;">S$<%out.print(wine.getPrice() * (100 - wine.getDiscount()) / 100);%></b>
                    </td>
                    <%} else {%>
                    <td style="text-align: center; vertical-align: middle;">S$<%out.print(wine.getPrice());%></td>
                    <%}%>
                    <td style="text-align: center; vertical-align: middle;"><%out.print(wineQuality);%></td>
                    <% DecimalFormat df = new DecimalFormat("#.00"); %>
                    <td style="text-align: center; vertical-align: middle;">S$<%out.print(df.format(payment));%></td>
                    <%sum = sum + payment;%>
                    <td style="width: 5%;"><form action="/WineXpressClient/cart/viewCart"><input name="deleteid" id="deleteid" type="hidden" value="<% out.print(wine.getId()); %>"><input name="deleteAction" id="deleteAction" type="hidden" value="1"><input style="vertical-align: central; text-align: center;" class="btn btn-warning" type="submit" value="Remove"></form>
                        <button class="btn btn-info" data-toggle="modal" data-target=".bs-modal-<% out.print(wine.getId());%>">Edit Amount</button>
                    </td>
                </tr>

                <div class="modal fade bs-modal-<% out.print(wine.getId());%>" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" style=" padding-top: 15%;">
                    <div class="modal-dialog modal-sm">
                        <div class="modal-content" style="padding-left: 20px; padding-right: 20px; width: 350px; height: 60px;">
                            <form action="/WineXpressClient/cart/viewCart">
                                <b>New Quantity:</b> &nbsp;
                                <input id="newamount" name="newamount" style="width: 100px;" />
                                <input id="editid" name="editid" value="<% out.print(wine.getId()); %>" type="hidden"/>
                                <input name="editAction" id="editAction" type="hidden" value="1" />&nbsp;
                                <input style="vertical-align: central; text-align: center;" class="btn btn-primary" type="submit" value="Update">
                            </form>
                        </div>
                    </div>
                </div>

                <%}%>
                <% DecimalFormat df = new DecimalFormat("#.00"); %>
                <tr><td colspan="5" style="text-align: right;"><b>Total:&nbsp;&nbsp;</b></td><td>S$<%out.print(df.format(sum));%></td></tr>
            </table>

            <div style="height: 40px;">
                <div style=" width: 92%; float: left; height: 40px;"></div><div style="width: 8%; float: left;">
                    <form action="/WineXpressClient/cart/makePayment" method="POST">
                        <input id="checkout" name="checkout" value="1" type="hidden">
                        <input id="paymentSum" name="paymentSum" value="<%=sum%>" type="hidden">
                        <button type ="submit" class="btn btn-primary"> Check Out</button>
                    </form>
                </div>
            </div>
            <%} else {%>
            <br/><h3 align="center"><i>No Product in Shopping Cart</i></h3>
            <h4 align="center"><a href="/WineXpressClient/shop.jsp"><i>Go back to wine list.</i></a></h4>
            <%}%>
        </div>
        <script>
            if (${alert1} == "1")
                alert("Please login before checkout!");
        </script>
        <t:footer/>
    </body>
</html>